package com.soft.collateral.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soft.collateral.entity.*;
import com.soft.collateral.mapper.*;
import com.soft.collateral.service.RecordService;
import com.soft.collateral.vo.PageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class RecordServiceImpl extends ServiceImpl<StorageWithdrawRecordMapper, StorageWithdrawRecord> implements RecordService {

    @Autowired
    private StorageWithdrawRecordMapper recordMapper;
    @Autowired
    private CollateralMapper collateralMapper;
    @Autowired
    private CollateralTypeMapper collateralTypeMapper;
    @Autowired
    private CollateralDetailMapper collateralDetailMapper;
    @Autowired
    private CollateralPicMapper collateralPicMapper;

    @Override
    public List<StorageWithdrawRecord> listAll() {
        List<StorageWithdrawRecord> records = recordMapper.selectList(null);
        records.forEach(this::loadDetails);
        return records;
    }

    @Override
    public Page<StorageWithdrawRecord> pageQuery(PageQueryVO pageQueryVO) {
        Page<StorageWithdrawRecord> page = new Page<>(pageQueryVO.getCurrentPage(), pageQueryVO.getPageSize());
        LambdaQueryWrapper<StorageWithdrawRecord> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(pageQueryVO.getQueryString())) {
            wrapper.like(StorageWithdrawRecord::getOperator, pageQueryVO.getQueryString());
        }
        Page<StorageWithdrawRecord> recordPage = recordMapper.selectPage(page, wrapper);
        recordPage.getRecords().forEach(this::loadDetails);
        return recordPage;
    }

    @Override
    public StorageWithdrawRecord getByIdWithDetails(Integer id) {
        StorageWithdrawRecord record = recordMapper.selectById(id);
        if (record != null) {
            loadDetails(record);
        }
        return record;
    }

    private void loadDetails(StorageWithdrawRecord record) {
        if (record.getCollateralId() != null) {
            Collateral collateral = collateralMapper.selectById(record.getCollateralId());
            if (collateral != null) {
                loadDetails(collateral);
            }
            record.setCollateral(collateral);
        }
    }

    private void loadDetails(Collateral collateral) {
        // 加载类型信息
        if (collateral.getTypeId() != null) {
            collateral.setCollateralType(collateralTypeMapper.selectById(collateral.getTypeId()));
        }
        // 加载详细信息
        CollateralDetail detail = collateralDetailMapper.selectOne(
                new LambdaQueryWrapper<CollateralDetail>()
                        .eq(CollateralDetail::getCollateralId, collateral.getCollateralId()));
        collateral.setCollateralDetail(detail);
        // 加载图片信息
        List<CollateralPic> pics = collateralPicMapper.selectList(
                new LambdaQueryWrapper<CollateralPic>()
                        .eq(CollateralPic::getCollateralId, collateral.getCollateralId())
                        .orderByAsc(CollateralPic::getId));
        collateral.setCollateralPic(pics);
    }
}